const fs = require('fs-extra')
const path = require('path')
const low = require('lowdb')
const envPaths = require('env-paths')
const paths = envPaths('dida-cli');
const FileSync = require('lowdb/adapters/FileSync');  // 有多种适配器可选择

// 确保该文件存在
fs.ensureDirSync(paths.config)

const configPath = path.join(paths.config, 'config.json')

const adapter = new FileSync(configPath); // 申明一个适配器

const db = low(adapter);

db.defaults({
  username: '',
  password: '',
  headless: 'No',
  classList: []
})
  .write();

module.exports = {
  // 初始化配置信息
  initConfig ({ username, password, headless, classList }) {
    return db
      .set('username', username)
      .set('password', password)
      .set('headless', headless)
      .set('classList', classList)
      .write()
  },
  // 获取配置信息
  getConfig () {
    const username = db.get('username').value()
    const password = db.get('password').value()
    const headless = db.get('headless').value()
    const classList = db.get('classList').value()
    return {
      username, password, headless, classList
    }
  },
}
